package com.kingdee.eas.cp.bc.travel;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.eventbus.event.action.KDActionEvent;
import com.kingdee.bos.webframework.exception.WafException;
import com.kingdee.bos.webframework.json.JSONUtils;
import com.kingdee.bos.webframework.dynamic.handler.AbstractWebEventHandler;
import com.kingdee.bos.eventbus.event.action.IActionEventHandler;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.basedata.assistant.ExchangeRateInfo;
import com.kingdee.eas.basedata.org.CompanyOrgUnitFactory;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.cp.bc.BizCollUtil;
import com.kingdee.eas.custom.basedata.LodgingCostCollection;
import com.kingdee.eas.custom.basedata.LodgingCostFactory;
import com.kingdee.eas.custom.basedata.LodgingCostInfo;
import com.kingdee.eas.custom.basedata.inlandForeign;
import com.kingdee.eas.custom.basedata.timeIimit;

public class PutUpVerifyHandler extends AbstractWebEventHandler implements IActionEventHandler {
	public void onAction(KDActionEvent event) throws WafException,BOSException {
		boolean result=false;
		try{
			String data=event.getReqeustContext().getHttpServletRequest().getParameter("data");
			JSONObject json=JSONObject.parseObject(data);
			Date startDate=json.getDate("startDate"); //出发时间
	 		Date endDate=json.getDate("endDate");  //到达时间
	 		String to=json.getString("to");  //到达城市
			long day=(endDate.getTime()-startDate.getTime())/(24*60*60*1000L)+1; //出差天数
			long putUpDay=json.getLongValue("putUpDay"); //住宿天数
			if((day-1)==putUpDay){
				result=true;
			}else{
				String isTimeIimit="";
				if(day<=30){
					isTimeIimit = timeIimit.FTRUE_VALUE;
				}else if(day>30&&day<90){
					isTimeIimit = timeIimit.A_VALUE;
				}else{
					isTimeIimit = timeIimit.B_VALUE;
				}
				if(!isTimeIimit.equals(timeIimit.FTRUE_VALUE)&&json.getString("inlandForeign").equals(inlandForeign.FOREIGN_VALUE)){
				}else{
					EntityViewInfo view=new EntityViewInfo();
					FilterInfo filter=new FilterInfo();
					//获取住宿费标准
					filter.getFilterItems().add(new FilterItemInfo("city",to));
					if(json.getString("inlandForeign").equals(inlandForeign.INLAND_VALUE)){ //国内区分淡旺季和职务等级
						filter.getFilterItems().add(new FilterItemInfo("dutyGrade",json.getString("duty")));
					}
					filter.getFilterItems().add(new FilterItemInfo("inlandForeign",json.getString("inlandForeign")));
					view.setFilter(filter);
					LodgingCostCollection lcc=LodgingCostFactory.getRemoteInstance().getLodgingCostCollection(view);
					if(json.getString("inlandForeign").equals(inlandForeign.INLAND_VALUE)){ 
						SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						Calendar start = Calendar.getInstance();
						start.setTime(startDate);
						int startYear=start.get(Calendar.YEAR);  //入住日期 年
						
						Calendar end = Calendar.getInstance();
						end.setTime(endDate);
						int endYear=end.get(Calendar.YEAR);  //退房日期 年
						end.set(Calendar.DATE,end.get(Calendar.DATE)-1);
						endDate=sf.parse(sf.format(end.getTime()));
						
						Date checkInDate1=startDate;
						Date checkOutDate1=endDate;
						if(startYear<endYear){
							result=false;
						}else{
							for(int i=0;i<lcc.size();i++){
								LodgingCostInfo info=lcc.get(i);
								String beginMonth=info.getMonthOne().getValue();  //开始月
								String beginDay=info.getSunOne().getValue(); //开始日
								String endMonth=info.getMonthTow().getValue();  //结束月
								String endDay=info.getSunTow().getValue(); //结束日
								
								Date standardBegin=sf.parse(startYear+"-"+beginMonth+"-"+beginDay+" 00:00:00"); //标准开始时间
								Date standardend=sf.parse(startYear+"-"+endMonth+"-"+endDay+" 00:00:00"); //标准开始时间
								
			//					(endDate>='2018-7-15' and beginDate<='2018-06-30')  #不跨季
			//					(endDate>='2018-08-30' and beginDate<='2018-10-15') #跨季
								if(standardend.getTime()>=checkOutDate1.getTime()&&standardBegin.getTime()<=checkInDate1.getTime()){
									result=true;
									break;
								}
								
							}
						}
					}
				}
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Boolean> map=new HashMap<String, Boolean>();
		map.put("result", result);
		JSONUtils.SUCCESS(map);
	}
}